<template>
	<view>
		<view v-if='WEBCONFIG?.ex_status=="2"'>
			<view class="px-16  flex flex-column grid-gap-4  ">
				<view class="p-12 rounded-4 ">
					<textarea placeholder="输入你的想法..." placeholder-class="fs-16" v-model="form.content"
						class="w-p-100 h-100"></textarea>
				</view>
				<view>
					<view class="mb-8">
						<text class="fs-16">添加照片</text>
						<text class="fs-13 text-grey-4">(限9张)</text>
					</view>
					<y-uploads :borderRadius='16' v-model="imageList"></y-uploads>
				</view>

				<view>
					<view class="mb-16 flex flex-column">
						<text class="fs-16">选择分类</text>
						<view class="flex flex-wrap grid-gap-8 mt-8">
							<text v-for="(item,index) in category" @click="form.category_id=item.id"
								class="px-12 fs-12  rounded-4 border-blue-1 py-5 "
								:class="{'text-blue-1 ':form.category_id!=item.id,'text-white bg-blue-1 ':form.category_id==item.id}">{{item.name}}</text>
						</view>
					</view>
				</view>

				<view class="mt-50 rounded-30 w-p-100 bg-blue-1 text-white text-center py-8" @click="submit()">立即发布
				</view>
			</view>
		</view>
		<view v-if='WEBCONFIG?.ex_status=="1"'>
			小程序是一种不需要下载安装即可使用的应用，它实现了应用“触手可及”的梦想，用户扫一扫或者搜一下即可打开应用。也体现了“用完即走”的理念，用户不用关心是否安装太多应用的问题。应用将无处不在，随时可用，但又无需安装卸载。
		</view>
		<y-loading v-if='show' overlay></y-loading>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { $http, $page, $tool } from '@/utils';
	import { onLoad } from '@dcloudio/uni-app'

	import { useUserStore, useRefs, useWebConfigStore } from "@/stores/index";
	const { WEBCONFIG } = useRefs(useWebConfigStore());
	const imageList = ref([])
	const show = ref(false)
	const form = ref({
		images: [],
		video: '',
		content: '',
		category_id: 0
	})
	const category = ref([])
	onLoad(() => {
		getCategory()
		console.log(11)
		if (WEBCONFIG?.value.ex_status == "1") {
			console.log(1)
			uni.setNavigationBarTitle({
				title: ''
			})
		}
	})
	const getCategory = () => {
		$http.get('Moments/getCate').then(res => {
			category.value = res.data
		})
	}
	const submit = () => {
		show.value = true
		if (imageList.value.length > 0) {
			imageList.value.forEach(item => {
				$http.upload('Upload/upload', { filePath: item, name: 'file' }).then(res => {
					form.value.images.push(res.data.url)
					if (form.value.images.length == imageList.value.length) {
						add()
					}
				}).catch(err => {
					show.value = false
				})
			})
		} else {
			add()
		}
	}
	const add = () => {
		$http.post('Moments/create', form.value).then(res => {
			$tool.toast('发布成功');
			setTimeout(() => {
				$page.back()
			}, 500)
			show.value = false
		}).catch(err => {
			show.value = false
		})
	}
</script>

<style>
	page {
		background-color: white;
	}
</style>